home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / portage / bin / archive-conf next >
Encoding:
Text File  |  2006-06-30  |  3.3 KB  |  102 lines

  1. #!/usr/bin/python
  2. # Copyright 1999-2006 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. # $Id: /var/cvsroot/gentoo-src/portage/bin/archive-conf,v 1.7 2004/10/04 13:57:36 vapier Exp $
  5.  
  6. #
  7. # archive-conf -- save off a config file in the dispatch-conf archive dir
  8. #
  9. #  Written by Wayne Davison <gentoo@blorf.net> with code snagged from
  10. #  Jeremy Wohl's dispatch-conf script and the portage chkcontents script.
  11. #
  12.  
  13. import os, sys, string
  14. sys.path = ["/usr/lib/portage/pym"]+sys.path
  15.  
  16. import portage, dispatch_conf
  17.  
  18. FIND_EXTANT_CONTENTS  = "find %s -name CONTENTS"
  19.  
  20. MANDATORY_OPTS  = [ 'archive-dir' ]
  21.  
  22. try:
  23.     import fchksum
  24.     def perform_checksum(filename): return fchksum.fmd5t(filename)
  25. except ImportError:
  26.     import md5
  27.     def md5_to_hex(md5sum):
  28.         hexform = ""
  29.         for ix in xrange(len(md5sum)):
  30.             hexform = hexform + "%02x" % ord(md5sum[ix])
  31.         return string.lower(hexform)
  32.     
  33.     def perform_checksum(filename):
  34.         f = open(filename, 'rb')
  35.         blocksize=32768
  36.         data = f.read(blocksize)
  37.         size = 0L
  38.         sum = md5.new()
  39.         while data:
  40.             sum.update(data)
  41.             size = size + len(data)
  42.             data = f.read(blocksize)
  43.         return (md5_to_hex(sum.digest()),size)
  44.  
  45. def archive_conf():
  46.     args = []
  47.     content_files = []
  48.     md5_match_hash = {}
  49.  
  50.     options = dispatch_conf.read_config(MANDATORY_OPTS)
  51.  
  52.     for conf in sys.argv[1:]:
  53.         if not os.path.isabs(conf):
  54.             conf = os.path.abspath(conf)
  55.         args += [ conf ]
  56.         md5_match_hash[conf] = ''
  57.  
  58.     # Find all the CONTENT files in VDB_PATH.
  59.     content_files += os.popen(FIND_EXTANT_CONTENTS % (portage.root+portage.VDB_PATH)).readlines()
  60.  
  61.     # Search for the saved md5 checksum of all the specified config files
  62.     # and see if the current file is unmodified or not.
  63.     try:
  64.         todo_cnt = len(args)
  65.         for file in content_files:
  66.             file = file.rstrip()
  67.             try:
  68.                 contents = open(file, "r")
  69.             except IOError, e:
  70.                 print >> sys.stderr, 'archive-conf: Unable to open %s: %s' % (file, e)
  71.                 sys.exit(1)
  72.             lines = contents.readlines()
  73.             for line in lines:
  74.                 items = string.split(line)
  75.                 if items[0] == 'obj':
  76.                     for conf in args:
  77.                         if items[1] == conf:
  78.                             stored = string.lower(items[2])
  79.                             real = string.lower(perform_checksum(conf)[0])
  80.                             if stored == real:
  81.                                 md5_match_hash[conf] = conf
  82.                             todo_cnt -= 1
  83.                             if todo_cnt == 0:
  84.                                 raise "Break"
  85.     except "Break":
  86.         pass
  87.  
  88.     for conf in args:
  89.         archive = os.path.join(options['archive-dir'], conf.lstrip('/'))
  90.         if options['use-rcs'] == 'yes':
  91.             dispatch_conf.rcs_archive(archive, conf, md5_match_hash[conf], '')
  92.             dispatch_conf.rcs_archive_post_process(archive)
  93.         else:
  94.             dispatch_conf.file_archive(archive, conf, md5_match_hash[conf], '')
  95.             dispatch_conf.file_archive_post_process(archive)
  96.  
  97. # run
  98. if len(sys.argv) > 1:
  99.     archive_conf()
  100. else:
  101.     print >> sys.stderr, 'Usage: archive-conf /CONFIG/FILE [/CONFIG/FILE...]'
  102.